import axios from 'axios'
const http = 'http://www.shuoyeah.com/interface/'

//请求拦截
axios.interceptors.request.use((config) => {
  //请求之前重新拼装url
  config.url = http + config.url;

  return config;
});
axios.interceptors.response.use(res => {
  // 对响应数据做些什么
  return res
}, err => {
  // 对响应错误做些什么
  console.log('err', err.response) // 修改后
  return Promise.reject(err.response)
});
class Axios {
  request({ url, data = {}, method = "GET" }) {
    return new Promise((resolve, reject) => {
      this._request(url, resolve, reject, data, method);
    });
  }
  _request(url, resolve, reject, data = {}, method = "GET") {
    // document.getElementById('egg').before(url)
    let format = method.toLocaleLowerCase() === 'get' ? 'params' : 'data';
    let headers = {
      "content-type": "application/json"
    }
    /*if( sessionStorage.getItem('token')) {
      headers.token = sessionStorage.getItem('token')
    }*/
    axios({
      url: url,
      method: method,
      [format]: data,
      timeout:1000000000000,
      headers: headers
    }).then(res => {
      // resolve(res);
      if (res.status_code != 200) {
        resolve(res);
      }
    }).catch((err) => {
      reject(err);
    })
  }
}
export { Axios, http}
